#include "networkengine.h"

using namespace std;

NetworkEngine::NetworkEngine(unsigned int localport)
{
    port = localport;
    started = false;
    quit = false;
}

void NetworkEngine::setPort( unsigned int newPort )
{
    if (!started) port = newPort;
}

unsigned int NetworkEngine::_port( void ) const
{
    return port;
}

NetworkEngine::~NetworkEngine(void)
{
    started = false;
}

void NetworkEngine::stop(void)
{
    started = false;
    kill(childPid, SIGKILL);
}

bool NetworkEngine::netStarted( void )
{
    return started;
}

NetworkEngine::Pipes* NetworkEngine::start(void)
{
    started = true;
    int forkReturn;

    signal(SIGCHLD, SIG_IGN);
    forkReturn = fork();
    childPid = forkReturn;
    if (forkReturn<0)
    {
        cerr << "Error forking" << endl;
        return &com;
    }

    // execution du processus fils
    if (forkReturn==0)
    {
        cout << "Network engine started" << endl;
        string cmd;

        close(com.pipPC[1]);
        close(com.pipCP[0]);

        ServerSocket server(port);

        while (started)
        {
            ServerSocket new_sock;
            cout << "Waiting for connection..." << endl;
            server.accept(new_sock);
            cout << "server m_sock " << new_sock.m_sock << endl;
            new_sock << "CONNECTOK";
            cout << "Inbound client." << endl;
            new_sock >> cmd;
            cout << "Received command: " << cmd << endl;
            new_sock << "CMDOK";
            execCmd(cmd, new_sock);
//            cout << "sleeping" << endl;
//            sleep(2);
//            cout << "fin sleep" << endl;
            new_sock.close();
        }
        // debug du processus fils:
        cout << "Network Engine Stopping..." << endl;
    }
    else
    {
        close(com.pipPC[0]);
        close(com.pipCP[1]);
    }

    return &com;
}

bool NetworkEngine::execCmd(string cmd, ServerSocket& new_sock)
{
    if (cmd=="MSG")
    {
        string message;
        new_sock >> message;
        new_sock << "DATAOK";
        cout << "message reçu: " << message << endl;
        return true;
    }
    if (cmd=="ADDEDTOGROUP")
    {
        new_sock.recvFile("group-files-received");
        com.send('c', "ADDEDTOGROUP");
        return true;
    }
    if (cmd=="NEWGROUPFILE")
    {
        new_sock.recvFile("group-files-received");
        com.send('c', "NEWGROUPFILERECEIVED");
        return true;
    }
    if (cmd=="INDEXFILEREQUEST")
    {
        string filename;
        new_sock >> filename;
        new_sock << "OK";
        new_sock.sendFile(filename);
    }
    if (cmd=="SENDFILEREQUEST")
    {
        string filename;
        string cmd;
        new_sock >> filename;
        cout << filename << endl;
        new_sock.sendFile(filename);
        cout << "file sent" << endl;
        return true;
    }

    return false;
}

bool NetworkEngine::sendMsg(string host, unsigned int port, string msg)
{
    ClientSocket client(host, port);
    string servermsg;

    client >> servermsg;
    if (servermsg != "CONNECTOK")
        return false;

    client << "MSG";
    client >> servermsg;
    if (servermsg != "CMDOK")
        return false;

    client << msg;
    client >> servermsg;
    if (servermsg != "DATAOK")
        return false;

    return true;
}
